package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-03-01 12:02
 **/
public class LeetCode6 {

    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        List<Character>[] lists = new List[numRows];
        for (int i = 0; i < numRows; i++) {
            lists[i] = new ArrayList<>();
        }
        int cur = 0;
        //true/false: 向下/向上
        boolean flag = true;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            lists[cur].add(c);
            if (cur == numRows - 1) {
                flag = false;
            } else if (cur == 0) {
                flag = true;
            }
            if (flag) {
                ++cur;
            } else {
                --cur;
            }
        }
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            for (Character c : lists[i]) {
                ans.append(c);
            }
        }
        return ans.toString();
    }

}
